home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / bathints.zip / BH_12.DOC < prev    next >
Text File  |  1989-01-05  |  26KB  |  617 lines

  1.  
  2.                                BAT-HINT # 12
  3.  
  4. **************************************************************************
  5.  
  6.     from the BATHINTS library... part of the BATPOWER CONFERENCE from:
  7.  
  8.                      THE PAINFRAME OPUS/FIDO 261/1004
  9.  
  10.                     Baltimore, Maryland 1-301-488-7461
  11.  
  12. **************************************************************************
  13.  
  14.                           Piping and Redirection
  15.  
  16.  
  17. After a standard start of your computer, your operating system is set such
  18. that the "standard" input device is the keyboard (the "console") and the
  19. standard output device is the monitor (the screen, your video, looks like a
  20. T.V. but there's only one channel).  The device name that DOS adopts to
  21. define your keyboard is "con", the console.  Your video screen has no
  22. device name in DOS.  Normally, all input and output is channeled through
  23. these two devices.  But you can redirect both your normal input and your
  24. normal output to other devices.  Indeed, most of us do it each day when we
  25. redirect the standard output to the printer, rather than the monitor, when
  26. printing document files.
  27.  
  28. PC-DOS provides a variety of alternatives to these two devices.  You can
  29. redirect the standard input device to a file of information or to an
  30. auxillary computer terminal, and you can redirect the standard output to a
  31. file, a printer, a modem or just about anything that is connected to a port
  32. on your computer system.
  33.  
  34. Though the CTTY internal comand of command.com is not really a topic for
  35. discussion in this conference, users should be aware that this command can
  36. change both your standard input and output devices to a terminal connected
  37. to the computer.  The syntax for the CTTY command is:
  38.  
  39. CTTY device
  40.  
  41. where "device" is any standard DOS device name (i.e. aux, com1, com2, com3,
  42. con, nul, etc.).  The named device must be able to both input and output
  43. all operations.  Specifying:
  44.  
  45. CTTY con
  46.  
  47. returns the standard input and output devices to your normal keyboard and
  48. screen.  Specifying AUX passes standard input and output to peripherals
  49. attached to the first serial/parallel port, COM2 to the second
  50. serial/parallel port and so on.  NUL is a dummy device that can be used to
  51. pass information into the "bit-bucket"... the info goes nowhere.  Though
  52. the NUL device is most widely used in the inhibition of information
  53. normally passed to your standard output device (your screen) during batch
  54. file operation, it also finds wide use in modem operations and is used less
  55. discriminately in certain security-type operations.  Specifying:
  56.  
  57. CTTY nul
  58.  
  59. will effectively negate the normal keyboard and screen as standard I/O
  60. devices, disabling the user from entering commands at the keyboard.
  61.  
  62. The piping and redirection operators used by DOS are:
  63.  
  64.     <    >    |
  65.  
  66. None of the symbols shown above can be used in the naming of files or their
  67. extensions.  Generally, the < and > symbols are referred to as redirection
  68. symbols and the | symbol is referred to as a piping symbol.  Some
  69. authorities refer to all of the symbols as piping symbols.  In this
  70. discussion, the former terminology will be used.
  71.  
  72. Before considering any examples of how redirection may be employed, note
  73. the following device names, which are reserved by DOS and should not be
  74. used otherwise:
  75.  
  76.     AUX  the first serial or parallel port
  77.     COM1 the first serial port
  78.     COM2 the second serial port
  79.     COMx a serial port where "x" = 1, 2, 3, 4 etc.
  80.     CON  the console/keyboard/monitor; can be used as input or output
  81.          device(s)
  82.     LPT1 the first parallel port, a printer; equivalent to PRN
  83.     LPTx a parallel port, printer, where "x" = 1 or 2
  84.     PRN  the first parallel port (printer)
  85.     NUL  a dummy device
  86.  
  87. Although the device names shown above cannot be used in the creation of
  88. files, you can substitute these device names in place of filenames in
  89. certain circumstances.
  90.  
  91. Redirecting Standard Output
  92. ---------------------------
  93.  
  94. The three most common uses of redirecting standard output are:
  95.     1.  printing ASCII files
  96.     2.  creating ASCII files
  97.     3.  inhibiting output to the standard device (the screen)
  98. The differences between these three operations lie only the device name (or
  99. filename) employed on the right side of the > symbol and the command which
  100. precedes it.  For example, to print a directory listing of the current
  101. subdirectory, to a printer attached to parallel port 1, you could issue the
  102. command:
  103.     dir > lpt1           or
  104.     dir > prn
  105. Both commands are essentially equivalent, though you will find that if you
  106. use the later command for the first time during a DOS session, the system
  107. will prompt you for the name of the device (lpt1, lpt2, etc.).  In general,
  108. the output of the command on the left side of the > symbol will be
  109. redirected to the device (or file) specified on the right side of the >
  110. symbol.  To redirect the output of the dir command to a printer attached to
  111. serial port 2, you would only need to change the name of the device to
  112. which standard output is to be redirected, thus:
  113.     dir > com2
  114. Virtually any executable command may have its output redirected... though
  115. of course there are cases where you would not want to do that... such as
  116. redirecting the output of a program like Word Perfect to a file or
  117. device... that would simply be nonsense.
  118.  
  119. To create an ASCII file that contains the output of the dir command, you
  120. need only specify the name of the file.  For example, to redirect the
  121. output of the dir command to a file called dir.doc, you could issue the
  122. command:
  123.     dir > dir.doc
  124. You should exercise caution when redirecting standard output to a file with
  125. the > symbol, since although the file will be created if it does not
  126. already exist, the file will be overwritten if it does exist.  Thus when
  127. using redirection of standard output to a file it is best to test whether
  128. that file already exists... since you may not want its contents
  129. overwritten.  Consider the following batch file which tests for the
  130. existance of a file specified as the %1 parameter when the batch file is
  131. called, and bypasses the command to redirect the standard output to that
  132. file if that file exists:
  133.  
  134. DIRDOC.BAT
  135. ----------
  136.  
  137. echo off
  138. if exist %1 goto oops
  139. dir > %1
  140. goto end
  141. :oops
  142. echo %1 already exists!
  143. :end                   (include a carriage return at the end of this line)
  144.  
  145. The proper syntax for this batch file is:
  146.     dirdoc filename.ext
  147. where filename.ext is any valid filename with or without an extension
  148. (alphanumeric constructs following the name of a batch file on the command
  149. line are "replaceable parameters" and each is separated from the next by a
  150. space; in the example shown above, only one parameter was given,
  151. "filename.ext", and it is thus the %1 replaceable parameter).  Thus, to
  152. redirect to a file called list.txt you would enter the command:
  153.     dirdoc list.txt
  154. Notice that the batch file first tests for the existance of the file
  155. specified on the command line (the %1 variable), and if it does exist, the
  156. batch file jumps to the label "oops" and informs the user that the file
  157. already exists.  With that information, the user could then run the batch
  158. file again, but specifying a different filename for the standard output. 
  159. You should be aware that if no %1 variable was specified when the batch
  160. file was called (i.e. only the name of the batch file was given on the
  161. command line), the result would be a "file creation error" since the
  162. statement dir > %1 is attempting to redirect standard output to a device or
  163. file that was not specified.  This problem could be avoided by including
  164. another test before the test for existance... just test whether the %1
  165. variable was specified at the command line, as shown in the example below
  166. (you may want to refer to BatHint # 10 to refresh your memory about the IF
  167. subcommand):
  168.  
  169. DIRDOC2.BAT
  170. -----------
  171.  
  172. echo off
  173. if "%1"=="" goto novar
  174. if exist %1 goto oops
  175. dir > %1
  176. goto end
  177. :novar
  178. echo You did not specify a filename...
  179. echo The correct syntax is:
  180. echo        dirdoc filename.ext
  181. echo where filename.ext is an